package net.qhzw.vip.qiyu.listener;

import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.entity.MaintRpt;
import net.qhzw.vip.qiyu.service.MaintRptService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MaintRptListener {

    @Autowired
    private MaintRptService maintRptService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 报案超时取消死信队列
     * @param message 接收的信息
     */
    @RabbitListener(queues = "DEAD_LETTER_QUEUE_HALL_ORDER")
    public void cancelHallOrder(Message message) {
        String id = new String(message.getBody());
        log.info("报修订单30分钟未接单,自动取消: {}", id);
        MaintRpt maintRpt = maintRptService.getById(id);
        if (maintRpt == null) {
            return;
        }
        if (maintRpt.getStatus().intValue() == 1 || maintRpt.getStatus().intValue() == 2) {
            maintRpt.setStatus((byte) 7);
            maintRptService.updateById(maintRpt);
            redisTemplate.boundHashOps("maintQueue").delete(maintRpt.getUserId());
        }
    }


}
